#include "baseconnection.h"

BaseConnection::BaseConnection(QObject *parent) :
    QObject(parent)
{
    _ipAddress[0] = 255;
    _ipAddress[1] = 0;
    _ipAddress[2] = 0;
    _ipAddress[3] = 0;

    // Initializes the port to the HTTP port
    _portNumber = 0;
}

/*
  setPortNumber

  Desc - Sets the port number to connect to
*/
void BaseConnection::setPortNumber(int newPortNumber)
{
    _portNumber = newPortNumber;
}

/*
  setIPAddress

  Desc - Sets the IP Address to connect to
*/
void BaseConnection::setIPAddress(int *newIPAddress)
{
    _ipAddress[0] = *(newIPAddress);
    _ipAddress[1] = *(newIPAddress + 1);
    _ipAddress[2] = *(newIPAddress + 2);
    _ipAddress[3] = *(newIPAddress + 3);
}

/*
  getPortNumber

  Desc - Gets the current port number
*/
int BaseConnection::getPortNumber()
{
    return _portNumber;
}

/*
  getIPAddress

  Desc -
*/
int *BaseConnection::getIPAddress()
{
    int *ipAddress;
    ipAddress = new int[4];

    *(ipAddress) = _ipAddress[0];
    *(ipAddress + 1) = _ipAddress[1];
    *(ipAddress + 2) = _ipAddress[2];
    *(ipAddress + 3) = _ipAddress[3];

    return ipAddress;
}


// slots

/*
  runPingTest

  Desc - runs a ping test on the current IP address. The result is
         returned via a signal from a thread. Ensures that only one pingThread
         exists for the current connection at once
*/
void BaseConnection::runPingTest()
{
    _networkTestThread.runPingTest(_ipAddress);
}

/*
  passOnPingTestResults

  Desc - passes on the result of a pingThread ping test. Called by the pingThread
*/
void BaseConnection::passOnPingTestResults(float pingResult)
{
    emit pingTestCompleted(pingResult);
}
